package at.fhj.app.activity;

import java.io.StringReader;
import java.util.ArrayList;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import at.fhj.app.R;
import at.fhj.app.model.Mark;
import at.fhj.app.model.MarkContentHandler;

/**
 * Display a list of all marks
 * 
 * @TODO Adapt this to implement a ListActivity
 * 
 * @author Markus Deutsch <Markus.Deutsch.ITM09@fh-joanneum.at>
 */
public class MarksActivity extends Activity {
	private ListView listview;
	private ArrayList<Mark> marks;
	private ArrayList<Object> listitems;
	
	private static final int TYPE_SEPARATOR = 0;
	private static final int TYPE_MARK = 1;

    protected void onCreate(Bundle savedInstanceState){
    	// Set property for XML parser
    	System.setProperty("org.xml.sax.driver","org.xmlpull.v1.sax2.Driver");
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_marks);

        Bundle bundle = this.getIntent().getExtras();
		String values = bundle.getString("values");
		
		/**
		 * Parse marks.
		 */
		try {
			XMLReader xmlReader = XMLReaderFactory.createXMLReader();
			MarkContentHandler mch = new MarkContentHandler();
			xmlReader.setContentHandler(mch);
			xmlReader.parse(new InputSource(new StringReader(values)));
			values = mch.marks.toString();
			marks = mch.marks;
		} catch(Exception e) {
			values = getString(R.string.msg_error);
			e.printStackTrace();
		}
		
		/*
		 * The marks have to be loaded into a new array.
		 * The array consists of Strings (term names) and Marks objects.
		 * Right now, this is the easiest ways to draw separators for terms.
		 */
		listitems = new ArrayList<Object>();
		if(marks != null && marks.size() > 0){
			for(int i = 0; i < marks.size(); i++){
				if(i == 0 || !marks.get(i).getTerm().equals(marks.get(i-1).getTerm())){
					listitems.add(marks.get(i).getTerm());
				}
				listitems.add(marks.get(i));
			}
		}
		
		listview = (ListView) findViewById(R.id.listview);
		listview.setAdapter(new MarksAdapter());
			
    }
    
    /**
     * MarksAdapter is used to display Marks in a ListView.
     * 
     * @author Markus Deutsch <Markus.Deutsch.ITM09@fh-joanneum.at>
     *
     */
    private class MarksAdapter extends BaseAdapter {
    	private Mark current;

		public int getCount() {
			return listitems.size();
		}

		public Object getItem(int position) {
			return listitems.get(position);
		}

		public long getItemId(int position) {
			return position;
		}
		
		public int getItemViewType(int position) {
			return (listitems.get(position) instanceof String) ? TYPE_SEPARATOR : TYPE_MARK;
		}
		
		public int getViewTypeCount() {
			return 2;
		}
		
		public boolean isEnabled(int position) {
			return getItemViewType(position) != TYPE_SEPARATOR;
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {
			int img = 0;
			final int type = getItemViewType(position);
			
			/**
			 * Set a type flag.
			 */
			if(convertView == null) {
				final LayoutInflater inflater = LayoutInflater.from(MarksActivity.this);
				final int layout = type == TYPE_SEPARATOR ? R.layout.item_separator : R.layout.item_marks;
				convertView = inflater.inflate(layout, parent, false);
			} 

			if(type == TYPE_SEPARATOR){
				/**
				 * If it's a separator (and the array item is a String)
				 * set the array text (term) as content of the separator.
				 */
				((TextView) convertView.findViewById(R.id.separator)).setText((String) listitems.get(position));
			} else {
				/**
				 * Populate the regular mark item layout.
				 */
				current = (Mark) getItem(position);
				TextView course = ((TextView) convertView.findViewById(R.id.item1));
				ImageView mark = ((ImageView) convertView.findViewById(R.id.mark));
				
				/**
				 * Depending on the mark, there's a different drawable.
				 */
				switch(current.getMark()){
					case 1:
						img = R.drawable.mark_1;
						break;
					case 2:
						img = R.drawable.mark_2;
						break;
					case 3:
						img = R.drawable.mark_3;
						break;
					case 4:
						img = R.drawable.mark_4;
						break;
					case 5:
						img = R.drawable.mark_5;
						break;
				}

				course.setText(current.getCourse());
				mark.setImageResource(img);
			}
			

			return convertView;
		}
    	
    }
	
}
